package pageTest.login;

import constants.HostConstant;
import constants.UserInfoConstant;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

public class UserPage extends WebDriverUtils {

    private static final String USER_PAGE_URL = "http://" + HostConstant.HOST + ":5173/user";

    public UserPage() {
        super(INIT_URL);
    }


    @BeforeClass
    public void setUp() {
        driver.get(super.LOGIN_PAGE_URL);
        driver.findElement(By.cssSelector("#van-field-1-input")).sendKeys(UserInfoConstant.USER_ACCOUNT);
        driver.findElement(By.cssSelector("#van-field-2-input")).sendKeys(UserInfoConstant.USER_PASSWORD);
        driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round")).click();
        // 确保登录成功了(登录操作响应已经返回， cookie 被种上)
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }

    @BeforeMethod
    public void beforeEachTest() {
        driver.get(USER_PAGE_URL);
    }

    @Test
    public void pageRight() {
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis")));
        String topNavBarText = driver.findElement(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis")).getText();
        assert "我的".equals(topNavBarText);
        String quitButtonText = driver.findElement(By.cssSelector("#userPage > div:nth-child(5) > button > div > span")).getText();
        assert "退 出".equals(quitButtonText);
    }

    @Test
    public void buttonClickable() {
        String topNavBarSlc = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        // 头像区域点击后跳转至信息修改页面
        driver.findElement(By.cssSelector("#userPage > div.van-space.van-space--horizontal.van-space--align-center > div:nth-child(2) > div")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "修改信息"));
        driver.navigate().back();
        // “我的标签”可点击
        driver.findElement(By.cssSelector("#userPage > div:nth-child(2)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "标签选择"));
        driver.navigate().back();
        // “创建的队伍”可点击
        driver.findElement(By.cssSelector("#userPage > div.van-grid > div:nth-child(1) > div")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "我创建的队伍"));
        driver.navigate().back();
        // “加入的队伍”可点击
        driver.findElement(By.cssSelector("#userPage > div.van-grid > div:nth-child(2) > div")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "我加入的队伍"));
        driver.navigate().back();
        // “好友列表”可点击
        driver.findElement(By.cssSelector("#userPage > div.van-grid > div:nth-child(3) > div")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "我的好友"));
        driver.navigate().back();
        // “联系作者”可点击
        driver.findElement(By.cssSelector("#userPage > div.van-grid > div:nth-child(4) > div")).click();
        String buttonText = driver.findElement(By.cssSelector("#userPage > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button > div > span")).getText();
        assert "确认".equals(buttonText);
        driver.findElement(By.cssSelector("#userPage > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button")).click();
        // “我的信息”可点击
        driver.findElement(By.cssSelector("#userPage > div.van-cell.van-cell--center.van-cell--clickable")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "修改信息"));
    }

    @Test(priority = 999)
    public void logout() {
        driver.findElement(By.cssSelector("#userPage > div:nth-child(5) > button")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
